package com.acme.bankapp.domain.client;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.acme.bankapp.domain.account.AbstractAccount;
import com.acme.bankapp.domain.exception.AccountExistsException;


public class Client implements Serializable{
	private String name = "";
	private Gender gengerType = Gender.MALE;
	private Set<AbstractAccount> accounts = new HashSet<>();;
	
	public Client(AbstractAccount account) {
		super();
		this.accounts.add(account);
	}
	
	public Client(String name, Gender gengerType, AbstractAccount account) {
		this(account);
		this.name = name;
		this.gengerType = gengerType;
	}
	
	public Set<AbstractAccount> getAccounts() {
		return Collections.unmodifiableSet(accounts);
	}
	
	public void addAccount(AbstractAccount account) throws AccountExistsException {
        if (accounts.contains(account))
                throw new AccountExistsException("Account alredy exsist!");
        
        accounts.add(account);
}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Gender getGengerType() {
		return gengerType;
	}
	public void setGengerType(Gender gengerType) {
		this.gengerType = gengerType;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(name + " " + gengerType);
		sb.append(" has " + accounts.size());
		return sb.toString();
	}

	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (gengerType != other.gengerType)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((gengerType == null) ? 0 :gengerType.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	

}


